<template>
  <div class="app-body">
    <div v-for="item in list">
      <div class="title">{{item.title}} </div>
      <div v-html="item.content"></div>
    </div>
    <div class="btn-group">
      <div class="btn" v-if="page>0" @click="pre()">上一篇</div>
      <div class="btn-gray" v-if="page==0" >上一篇</div>
      <div class="btn"  v-if="page<pageCount" @click="next()">下一篇</div>
      <div class="btn-gray" v-if="page==pageCount" >下一篇</div>

    </div>
  </div>
</template>
<script>
import addEditPadNewsView from "@/components/pad/addEditPadNews.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";

export default {
  name: "PadNewsListView",
  data() {
    return {
      type: 0,
      title: "",
      page: 0,
      pageSize: 1,
      total: 0,
      limit: 1,
      list: [],
      form: {
        title: "",
        imageUrl: "",
        publishTime: "",
        publishUser: "",
        createNumber: "",
        createName: "",
        laseUpdateNumber: "",
        laseUpdateName: "",
      },
    };
  },
  components: {
    addEditPadNewsView,
    addFileView,
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage,
    Upload,
  },
  setup() {
    return {
      locale: zhCn,
    };
  },
  mounted() {
    this.getPadNewsList();
  },
  watch: {
    $route() {
      this.getPadNewsList();
    },
  },
  methods: {
    next(){
      this.page = this.page+1;
      this.getPadNewsList();
    },
    pre(){
      this.page = this.page-1;
      this.getPadNewsList();
    },
    searchEnter: function (e) {
      // 使用 which 和 keyCode 属性来解决兼容问题
      var keyCode = window.event ? e.keyCode : e.which;
      var val = e.target.value;
      if (keyCode == 13 && val) {
        this.getPadNewsList();
      }
    },
    add() {
      this.$refs.addEditPadNewsView.title = "添加时政新闻";
      this.$refs.addEditPadNewsView.initTinymce();
      this.$refs.addEditPadNewsView.dialogFormVisible = true;
    },
    editPadNews(obj) {
      this.$refs.addEditPadNewsView.title = "修改时政新闻";
      this.$refs.addEditPadNewsView.getPadNewsById(obj.id);
      this.$refs.addEditPadNewsView.dialogFormVisible = true;
    },
    deletePadNews(id) {
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService("/javaApi/padNews/deleteById", "id=" + id, "post").then(
            (data) => {
              if (data.code == 200) {
                ElMessage.success("删除成功");
                this.getPadNewsList();
              } else {
                ElMessage.error("删除失败，请重试！");
              }
            }
          );
        })
        .catch(() => {});
    },
    reloadData() {
      this.page = 1;
      this.getPadNewsList();
    },
    importExcel() {
      this.$refs.addFileView.dialogFormVisible = true;
      this.$refs.addFileView.title = "批量导入时政新闻表";
      this.$refs.addFileView.uploadApiUrl = "/javaApi/padNews/importExcel";
    },
    exportExcel() {
      httpService(
        "/javaApi/padNews/exportExcel",
        {
          title: this.form.title,
          imageUrl: this.form.imageUrl,
          publishTime: this.form.publishTime,
          publishUser: this.form.publishUser,
          createNumber: this.form.createNumber,
          createName: this.form.createName,
          laseUpdateNumber: this.form.laseUpdateNumber,
          laseUpdateName: this.form.laseUpdateName,
        },
        "get"
      ).then((data) => {
        if (data.code == 200) {
          window.open(data.msg, "_blank");
        }
      });
    },
    onSubmit() {
      this.getPadNewsList();
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.pageSize = size;
      this.limit = size;
      this.getPadNewsList();
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getPadNewsList();
    },
    tableRowClassName({ row, rowIndex }) {
      return "";
    },
    getPadNewsList() {
      httpService(
        "/javaApi/android/newsList/" + this.page + "/" + this.pageSize,
        {
          title: this.form.title,
          imageUrl: this.form.imageUrl,
          publishTime: this.form.publishTime,
          publishUser: this.form.publishUser,
          createNumber: this.form.createNumber,
          createName: this.form.createName,
          laseUpdateNumber: this.form.laseUpdateNumber,
          laseUpdateName: this.form.laseUpdateName,
        },
        "get"
      ).then((data) => {
        this.list = data.list;
        this.total = data.total;
        this.pageCount = data.pages;
      });
    },
  },
};
</script>
<style scoped>
.btn-group{
  position: fixed;
  display: flex;
  left: 0;
  right: 0;
  padding: 20px;
  background: white;
  bottom: 0;
  justify-content: space-between;

}
.title{
  font-size: 20px;
  font-weight: 500;
}
.btn{
  color: red;
  cursor: pointer;
}
.btn-gray{
  color: gray;
}
.el-main {
  padding: 0px;
}

.teacher-content {
  width: 100%;
}

.app-table {
  background: #eff3f5;
  padding: 10px;
}

.app-search {
}

.app-separator {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 10px;
}

.app-body {
  min-height: calc(100vh - 204px);

  background: white;
  margin-bottom: 10px;

  padding: 20px 20px 10px 20px;
}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
  text-align: center;
  margin-top: 10px;
}
</style>

